//
//  SYJiaoCaiDetailView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2020/2/26.
//  Copyright © 2020年 SanYi. All rights reserved.
//

#import "SYJiaoCaiDetailView.h"
#import "SYTopLabView.h"
#import "SYJiaoCaiDetailsTableViewCell.h"
#import "SYShareBanJiListView.h"
#import "SYPlayerViewController.h"
#import "SYPPTViewController.h"


@interface SYJiaoCaiDetailView ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) SYBackTopView *backView;//顶部viwe
@property (strong, nonatomic) SYTopLabView *topLabView;
@property (strong, nonatomic) UITableView *curTableView;//
@property (strong, nonatomic) NSMutableArray *datalistArr;
@property (assign, nonatomic) NSInteger index;;
@property (copy, nonatomic) NSString *packageId;//选择资源包id

//@property (strong, nonatomic) SYJiaoCaiCell ;

@end
@implementation SYJiaoCaiDetailView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
        [self initSubView];
    }
    return self;
}
- (void)initSubView {
    [self addSubview:self.backView];
    [self.backView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(50);
    }];
    [self addSubview:self.topLabView];
    [self.topLabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.backView.mas_bottom);
        make.height.mas_equalTo(45);
    }];
    
    [self addSubview:self.curTableView];
    [self.curTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.topLabView.mas_bottom);
        make.bottom.equalTo(self.mas_bottom);
    }];
    
    _datalistArr = [NSMutableArray array];
    
    [_curTableView registerCellWithNIbNames:@[@"SYJiaoCaiDetailsTableViewCell"]];
    
}

- (void)requestDataGroupId:(NSString *)groupId {
    [MBProgressHUD mj_showLoading];
    [SYRequestAPI zy_requestGetResourceEmployeeListPackageId:groupId success:^(NSArray *arr) {
        [MBProgressHUD mj_hideHUD];
        [self.datalistArr addObjectsFromArray:arr];
        [self.curTableView reloadData];
        
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUD];
        
    }];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return _datalistArr.count;
//    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYJiaoCaiDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SYJiaoCaiDetailsTableViewCell" forIndexPath:indexPath];


    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//
    NSDictionary *dict = self.datalistArr[indexPath.row];
    cell.xuhaoLab.text = [dict nilObjectForKey:@"rowNum"];
    [cell.iconImage sd_setImageWithURL:[NSURL URLWithString:[dict nilObjectForKey:@"fileIcon"]] placeholderImage:[UIImage imageNamed:@""]];
    cell.biaotiLab.text = [dict nilObjectForKey:@"title"];
    cell.sizeLab.text = [dict nilObjectForKey:@"size"];
    cell.chuangjianrenLab.text = [dict nilObjectForKey:@"createUser"];
    NSString *timeStr = [dict nilObjectForKey:@"createDate"];
    timeStr = [timeStr substringFromIndex:timeStr.length - 3];//先这样写。后期有时间优化。标记 TOD
    cell.chuangjianTimeLab.text =  [[dict nilObjectForKey:@"createDate"] stringByReplacingOccurrencesOfString:timeStr withString:@""];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    NSDictionary *dict = self.datalistArr[indexPath.row];
    NSString *path = [dict nilObjectForKey:@"path"];//观看地址
    NSMutableString *type = [dict nilObjectForKey:@"type"];//类型
    if ([type integerValue] == 3) {
        SYPlayerViewController *playerVC = [[SYPlayerViewController alloc] init];
        playerVC.urlStr = path;
        playerVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:playerVC animated:YES completion:nil];

    } else {
        SYPPTViewController *wordVC = [[SYPPTViewController alloc] init];
        wordVC.urlStr = path;
        wordVC.nameStr = [dict nilObjectForKey:@"title"];
        wordVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:wordVC animated:YES completion:nil];

    }
    
    
    
//    SYPlayerViewController *playerVC = [[SYPlayerViewController alloc] init];
//    playerVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
//    playerVC.urlStr = model.path;
//    [self presentViewController:playerVC animated:YES completion:nil];
    
}
//头部
- (SYBackTopView *)backView {
    if (!_backView) {
        _backView = [[SYBackTopView alloc] initWithTitleName:@"教材包详情" baseView:self];
    }
    return _backView;
}
#pragma mark -
- (SYTopLabView *)topLabView {
    if (!_topLabView) {
        NSArray *xiaoArr = nil;
        CGFloat bili;
        
        xiaoArr = @[@"序号",@"素材图标",@"素材标题",@"素材大小",@"创建人",@"创建时间"];
        bili = 0.1666666666;
        
        
        _topLabView = [[SYTopLabView alloc] initWithDataNameArr:xiaoArr width:bili];
    }
    return _topLabView;
}

- (UITableView *)curTableView {
    if (!_curTableView) {
        _curTableView = [[UITableView alloc] init];
        _curTableView.delegate = self;
        _curTableView.dataSource = self;
        _curTableView.tableFooterView = [UIView new];
        
    }
    return _curTableView;
}
@end
